/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ThinkProjectSettingPanelVisual.java
 *
 * Created on 2009-2-6, 21:33:03
 */

package ThinkPhpPlugin.Project;

import java.util.prefs.Preferences;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;
import org.openide.WizardDescriptor;
import ThinkPhpPlugin.Setting.ThinkphpSettingPanel;
/**
 *
 * @author Administrator
 */
public class ThinkProjectSystemPanelVisual extends javax.swing.JPanel  implements DocumentListener {

    private ThinkProjectSystemWizardPanel panel;
    //public static final String PROP_PROJECT_NAME = "projectName";
    /** Creates new form ThinkProjectSettingPanelVisual */
    public ThinkProjectSystemPanelVisual(ThinkProjectSystemWizardPanel panel) {
        initComponents();
        this.panel = panel;

        //注册监听器
        db_host.getDocument().addDocumentListener(this);
        db_user.getDocument().addDocumentListener(this);
        db_name.getDocument().addDocumentListener(this);
        db_port.getDocument().addDocumentListener(this);
        db_prefix.getDocument().addDocumentListener(this);

        //装载默认设置
        Preferences root=Preferences.userNodeForPackage(ThinkphpSettingPanel.class);
        db_type.setSelectedIndex(root.getInt("db_type", 0));
        db_host.setText(root.get("db_host", "localhost"));
        db_user.setText(root.get("db_user", "root"));
        db_pwd.setText(root.get("db_pwd", ""));
        db_port.setText(root.get("db_port", "3306"));
        db_name.setText(root.get("db_name", "test"));
        db_prefix.setText(root.get("db_prefix", "think_"));
    }


    void read(WizardDescriptor wizardDescriptor) {
        //throw new UnsupportedOperationException("Not yet implemented");
    }

     @Override
    public void addNotify() {
        super.addNotify();
        //same problem as in 31086, initial focus on Cancel button
        //projectNameTextField.requestFocus();
    }
     
    void store(WizardDescriptor d) {
        //保存用户输入的值
        Preferences root=Preferences.userNodeForPackage(ThinkphpSettingPanel.class);
        root.putInt("db_type", db_type.getSelectedIndex());
        root.put("db_host", db_host.getText());
        root.put("db_port", db_port.getText());
        root.put("db_user", db_user.getText());
        root.put("db_pwd", db_pwd.getText());
        root.put("db_prefix", db_prefix.getText());
    }

    boolean valid(WizardDescriptor wizardDescriptor) {
        if (db_host.getText().length() == 0) {
            // TODO if using org.openide.dialogs >= 7.8, can use WizardDescriptor.PROP_ERROR_MESSAGE:
            wizardDescriptor.putProperty("WizardPanel_errorMessage",
                    "数据库主机不能为空，默认为localhost");
            return false; // Display name not specified
        }
        if (db_user.getText().length() == 0) {
            // TODO if using org.openide.dialogs >= 7.8, can use WizardDescriptor.PROP_ERROR_MESSAGE:
            wizardDescriptor.putProperty("WizardPanel_errorMessage",
                    "数据库用户名不能为空，默认为root");
            return false; // Display name not specified
        }

        if (db_name.getText().length() == 0) {
            // TODO if using org.openide.dialogs >= 7.8, can use WizardDescriptor.PROP_ERROR_MESSAGE:
            wizardDescriptor.putProperty("WizardPanel_errorMessage",
                    "数据库名不能为空");
            return false; // Display name not specified
        }

        if (db_port.getText().length() == 0) {
            // TODO if using org.openide.dialogs >= 7.8, can use WizardDescriptor.PROP_ERROR_MESSAGE:
            wizardDescriptor.putProperty("WizardPanel_errorMessage",
                    "数据库主机端口不能为空，默认为3306");
            return false; // Display name not specified
        }

        wizardDescriptor.putProperty("WizardPanel_errorMessage","");

        return true;
    }

    void validate(WizardDescriptor wizardDescriptor) {
        //throw new UnsupportedOperationException("Not yet implemented");
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        db_type = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        db_host = new javax.swing.JTextField();
        db_user = new javax.swing.JTextField();
        db_pwd = new javax.swing.JTextField();
        db_name = new javax.swing.JTextField();
        db_port = new javax.swing.JTextField();
        db_prefix = new javax.swing.JTextField();

        db_type.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "mysql", "mysqli" }));

        jLabel1.setText(org.openide.util.NbBundle.getMessage(ThinkProjectSystemPanelVisual.class, "ThinkProjectSystemPanelVisual.jLabel1.text")); // NOI18N

        jLabel2.setText(org.openide.util.NbBundle.getMessage(ThinkProjectSystemPanelVisual.class, "ThinkProjectSystemPanelVisual.jLabel2.text")); // NOI18N

        jLabel3.setText(org.openide.util.NbBundle.getMessage(ThinkProjectSystemPanelVisual.class, "ThinkProjectSystemPanelVisual.jLabel3.text")); // NOI18N

        jLabel4.setText(org.openide.util.NbBundle.getMessage(ThinkProjectSystemPanelVisual.class, "ThinkProjectSystemPanelVisual.jLabel4.text")); // NOI18N

        jLabel5.setText(org.openide.util.NbBundle.getMessage(ThinkProjectSystemPanelVisual.class, "ThinkProjectSystemPanelVisual.jLabel5.text")); // NOI18N

        jLabel6.setText(org.openide.util.NbBundle.getMessage(ThinkProjectSystemPanelVisual.class, "ThinkProjectSystemPanelVisual.jLabel6.text")); // NOI18N

        jLabel7.setText(org.openide.util.NbBundle.getMessage(ThinkProjectSystemPanelVisual.class, "ThinkProjectSystemPanelVisual.jLabel7.text")); // NOI18N

        db_host.setText(org.openide.util.NbBundle.getMessage(ThinkProjectSystemPanelVisual.class, "ThinkProjectSystemPanelVisual.db_host.text")); // NOI18N

        db_user.setText(org.openide.util.NbBundle.getMessage(ThinkProjectSystemPanelVisual.class, "ThinkProjectSystemPanelVisual.db_user.text")); // NOI18N

        db_pwd.setText(org.openide.util.NbBundle.getMessage(ThinkProjectSystemPanelVisual.class, "ThinkProjectSystemPanelVisual.db_pwd.text")); // NOI18N

        db_name.setText(org.openide.util.NbBundle.getMessage(ThinkProjectSystemPanelVisual.class, "ThinkProjectSystemPanelVisual.db_name.text")); // NOI18N

        db_port.setText(org.openide.util.NbBundle.getMessage(ThinkProjectSystemPanelVisual.class, "ThinkProjectSystemPanelVisual.db_port.text")); // NOI18N

        db_prefix.setText(org.openide.util.NbBundle.getMessage(ThinkProjectSystemPanelVisual.class, "ThinkProjectSystemPanelVisual.db_prefix.text")); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                                .addComponent(jLabel2)
                                .addComponent(jLabel1))
                            .addComponent(jLabel6)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addGap(4, 4, 4)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(db_host)
                            .addComponent(db_port)
                            .addComponent(db_name)
                            .addComponent(db_pwd)
                            .addComponent(db_user)
                            .addComponent(db_type, 0, 135, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(db_prefix)))
                .addContainerGap(254, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(db_type))
                .addGap(16, 16, 16)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(db_host))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(db_user)
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(db_pwd)
                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(db_name)
                    .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(db_port)
                    .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, 21, Short.MAX_VALUE)
                    .addComponent(db_prefix, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField db_host;
    private javax.swing.JTextField db_name;
    private javax.swing.JTextField db_port;
    private javax.swing.JTextField db_prefix;
    private javax.swing.JTextField db_pwd;
    private javax.swing.JComboBox db_type;
    private javax.swing.JTextField db_user;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    // End of variables declaration//GEN-END:variables

    // Implementation of DocumentListener --------------------------------------
    public void changedUpdate(DocumentEvent e) {
        updateTexts(e);
    }

    public void insertUpdate(DocumentEvent e) {
        updateTexts(e);
    }

    public void removeUpdate(DocumentEvent e) {
        updateTexts(e);
    }

    /** Handles changes in the Project name and project directory, */
    private void updateTexts(DocumentEvent e) {
        /*Document doc = e.getDocument();
        if (doc == db_name.getDocument()) {
            String projectName = db_name.getText();
        }*/
        panel.fireChangeEvent(); // Notify that the panel changed
    }
}
